<?php
/**
 * Created LI
 * Date: 2016/4/10
 * Time: 10:48
 */
namespace Admin\Controller;
use Think\Controller;
use Think\Upload;
class PetController extends Controller{
    public function __construct()
    {
        parent::__construct();
        if(!$_SESSION['admin_login']){
            $this->error('请登录','Index/login');
        }
    }

    public function ency(){
        $model_ency = D('PetEncy');
        $model_cate = D('Category');

        $condition = array();
        if($_POST['pet_name']){
            $condition['Name'] = $_POST['pet_name'];
        }
        if($_POST['category']) {
            $condition['Category'] = $_POST['category'];
        }
        $ency_list = $model_ency -> getPetEncyList($condition);
        foreach ($ency_list as $k => $v){
            $cate_info = $model_cate -> getCategoryInfo(array('Id' => $v['category']));
            $ency_list[$k]['category'] = $cate_info['name'];
        }
        //宠物分类列表
        $cate_list = $model_cate ->getCategoryList();
        $this -> assign('cate_list',$cate_list);
        $this -> assign('ency_list',$ency_list);
        self::profile_menu('list');
        $this -> display('pet_ency.list');
    }
    public function ency_edit(){
        $model_ency = D('PetEncy');
        $model_cate = D('Category');
        $model_feature = D('Feature');
        $model_color = D('HairColor');
        if($_POST['submit'] == 'OK'){
            $id = $_POST['id'];
            $ency_info  = $model_ency -> getPetEncyInfo(array('id' => $id));
            $upload = new Upload();
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =     './Public/img/Pet/'; // 设置附件上传根目录
            $upload->savePath  =     ''; // 设置附件上传（子）目录

            // 上传文件
            if(!empty($_FILES['photo'])) {
                $upload->saveName  =     time().'_'.mt_rand();
                $info = $upload->uploadOne($_FILES['photo']);
                if (!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                } else {// 上传成功
                    $_POST['photo'] = $info['savename'];
                }
            }
            $condition = array('Id' => $id);
            $param = array(
                'Name' => $_POST['name'],
                'Category' => $_POST['category'],
                'Introduce' => $_POST['introduce'],
                'HairId' => $_POST['haircolor'],
                'Feature' => $_POST['feature'],
                'photo' => $_POST['photo'] ? $_POST['photo'] : $ency_info['photo'],
                'ForeignName' => $_POST['foreginname'],
                'OtherName' => $_POST['othername'],
                'ChanDi' => $_POST['chandi'],
                'ShouMing' => $_POST['shouming'],
            );
            $result = $model_ency ->editPetEncy($param, $condition);
            if($result){
                $this ->success('编辑成功！','ency');
            }else{
                $this ->error('编辑失败，请核对信息','ency_edit/id/'.$id);
            }
        }
        $id = $_GET['id'];
        $ency_info  = $model_ency -> getPetEncyInfo(array('id' => $id));
        //种类列表
        $cate_list = $model_cate ->getCategoryList();
        $this -> assign('cate_list',$cate_list);
        //特性列表
        $feature_list = $model_feature ->getFeatureList();
        $this -> assign('feature_list',$feature_list);
        //毛色列表
        $color_list = $model_color ->getHairColorList();
        $this -> assign('color_list',$color_list);
        $this -> assign('ency',$ency_info);
        self::profile_menu('edit');
        $this -> display('pet_ency.edit');
    }
    /**
     * 添加宠物百科信息
     */
    public function addPet(){
        $model_ency = D('PetEncy');
        $model_cate = D('Category');
        $model_feature = D('Feature');
        $model_color = D('HairColor');
        if($_POST['submit'] == 'OK'){
            $upload = new Upload();
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =     './Public/img/Pet/'; // 设置附件上传根目录
            $upload->savePath  =     ''; // 设置附件上传（子）目录

            // 上传文件
            if(!empty($_FILES['photo'])) {
                $upload->saveName  =     time().'_'.mt_rand();
                $info = $upload->uploadOne($_FILES['photo']);
                if (!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                } else {// 上传成功
                    $_POST['photo'] = $info['savename'];
                }
            }else{
                $this ->error('请上传图片','addPet');
            }
            $param = array(
                'Name' => $_POST['name'],
                'Category' => $_POST['category'],
                'Introduce' => $_POST['introduce'],
                'HairId' => $_POST['haircolor'],
                'Feature' => $_POST['feature'],
                'photo' => $_POST['photo'],
                'ForeignName' => $_POST['foreginname'],
                'OtherName' => $_POST['othername'],
                'ChanDi' => $_POST['chandi'],
                'ShouMing' => $_POST['shouming'],
            );
            $result = $model_ency ->addPetEncy($param);
            if($result){
                $this ->success('添加成功！','ency');
            }else{
                $this ->error('添加失败，请核对信息','addPet');
            }
        }
        //种类列表
        $cate_list = $model_cate ->getCategoryList();
        $this -> assign('cate_list',$cate_list);
        //特性列表
        $feature_list = $model_feature ->getFeatureList();
        $this -> assign('feature_list',$feature_list);
        //毛色列表
        $color_list = $model_color ->getHairColorList();
        $this -> assign('color_list',$color_list);
        self::profile_menu('add');
        $this -> display('pet_ency.edit');
    }
    /**
     * 删除百科
     */
    public function del(){
       if($_POST['id'] == 0){
           echo 'false';
       }
        $model_ency = D('PetEncy');
        $result = $model_ency -> delPet(array('Id' => $_POST['id']));
        if($result){
            echo 'true';
        }else{
            echo 'false';
        }
    }
    /**
     * 导航
     */
    private function profile_menu($key){
        $menu_array = array(
            'pet_ency'=>array('menu_key'=>'pet_ency','menu_name'=>'宠物百科管理','menu_url'=>'index.php/admin/pet/ency'),
        );
        switch($key){
            case 'list':
                $menu_array['pet_ency']['child'] = array('menu_key'=>'pet_ency','menu_name'=>'百科查询','menu_url'=>'index.php/admin/pet/ency');
                break;
            case 'add':
                $menu_array['pet_ency']['child'] = array('menu_key'=>'pet_add','menu_name'=>'百科添加','menu_url'=>'index.php/admin/pet/addPet');
                break;
            case 'edit':
                $menu_array['pet_ency']['child'] = array('menu_key'=>'ency_edit','menu_name'=>'百科编辑','menu_url'=>'index.php/admin/pet/ency_edit');
                break;
        }
        $this -> assign('menu_array',$menu_array);
    }
}